package com.ccsoft.leetcode.service;

/**
 * 35. 搜索插入位置 - 二分法查找插入位置
 */
public class Dichotomy {
    public int searchInsert(int[] nums, int target) {
        int n = nums.length;
        int left = 0, right = n - 1, ans = n;
        while(left <= right){
            int mid = ((right - left) >> 1) + left;
            if( target <= nums[mid] ){
                ans = mid;
                right = mid - 1;
            } else {
                left = mid + 1;
            }
        }
        return ans;
    }
}
